package com.xx.happychatboot.middleware.springsecurity.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.xx.happychatboot.middleware.springsecurity.domain.vo.response.AuthResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 权限拦截器
 */
@Component
public class MyAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response,
                       AccessDeniedException accessDeniedException) throws IOException, ServletException {
        AuthResponse authResponse = new AuthResponse(HttpServletResponse.SC_FORBIDDEN,"您没有权限");
        response.setContentType("application/json;charset=UTF-8");
        String s = new ObjectMapper().writeValueAsString(authResponse);
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);
        response.getWriter().println(s);
    }
}
